package com.cnf.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnf.reggie.common.R;
import com.cnf.reggie.entity.Category;
import com.cnf.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Category category) {
        log.info("新增菜品分类，菜品分类信息：{}", category);
        categoryService.save(category);
        return R.success("新增成功！！！！");
    }
    @GetMapping("/page")
    public R<Page> page( int page, int pageSize) {
        Page<Category> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> page(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改成功！！！");
    }
    @DeleteMapping
    public R<String> delete( Long id) {
        log.info("删除的ID为：{}",id);
//        categoryService.removeById(id);
        categoryService.remove(id);
        return R.success("删除成功！！！");
    }
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(category.getType() != null,Category::getType,category.getType() );
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lambdaQueryWrapper);
        return R.success(list);
    }
}
